package cn.sdhuijin.healthexam.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.control.AbstractControlActivity;
import cn.sdhuijin.healthexam.util.Util;

/**
 * 档案信息显示
 * 
 * 修改完成
 * 
 * @author sunping
 * 
 */
public class ArchivesInfoActivity extends AbstractControlActivity implements
		OnClickListener {

	private TextView operation; // 手术
	private TextView trauma; // 外伤
	private TextView transfusionBlood; // 输血

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_archives_info);
		ExitApplication.getInstance().addActivity(this);

		Intent intent = getIntent();
		// 获取传递过来的sql语句
		String fileName = intent.getStringExtra("filename");

		addTextView(R.id.txt_archivesInfo_name, "姓名");
		addTextView(R.id.txt_archivesInfo_sex, "性别_content");
		addTextView(R.id.txt_archivesInfo_birthDate, "出生日期");
		addTextView(R.id.txt_archivesInfo_cardId, "身份证号");
		addTextView(R.id.txt_archivesInfo_workUnit, "工作单位");
		addTextView(R.id.txt_archivesInfo_home, "家庭地址");
		addTextView(R.id.txt_archivesInfo_selfPhone, "联系电话");
		addTextView(R.id.txt_archivesInfo_contactName, "紧急联系人");
		addTextView(R.id.txt_archivesInfo_ContactPhone, "联系人电话");
		addTextView(R.id.txt_archivesInfo_HjType, "常住类型_content");
		addTextView(R.id.txt_archivesInfo_nation, "民族");
		addTextView(R.id.txt_archivesInfo_bloodType, "血型_content");
		addTextView(R.id.txt_archivesInfo_rhNegative, "RH阴性_content");
		addTextView(R.id.txt_archivesInfo_education, "文化程度_content");
		addTextView(R.id.txt_archivesInfo_profession, "职业_content");
		addTextView(R.id.txt_archivesInfo_marry, "婚姻状况_content");
		addTextView(R.id.txt_archivesInfo_payType, "医药支付_content");
		addTextView(R.id.txt_archivesInfo_allergies, "药物过敏_content");
		addTextView(R.id.txt_archivesInfo_exposure, "暴露史_content");
		addTextView(R.id.txt_archivesInfo_disease, "疾病史_content");
		addTextView(R.id.txt_archivesInfo_familyHistory_fuqin,
				"家族病史_父亲_content");
		addTextView(R.id.txt_archivesInfo_familyHistory_muqin,
				"家族病史_母亲_content");
		addTextView(R.id.txt_archivesInfo_familyHistory_xiongdi,
				"家族病史_兄弟姐妹_content");
		addTextView(R.id.txt_archivesInfo_familyHistory_zinv, "家族病史_子女_content");
		addTextView(R.id.txt_archivesInfo_geneticHistory, "遗传病名称");
		addTextView(R.id.txt_archivesInfo_disability, "残疾情况_content");
		addTextView(R.id.txt_archivesInfo_kitchenVentilation, "厨房排风设施_content");
		addTextView(R.id.txt_archivesInfo_fuelType, "燃料类型_content");
		addTextView(R.id.txt_archivesInfo_water, "饮水_content");
		addTextView(R.id.txt_archivesInfo_toilet, "厕所_content");
		addTextView(R.id.txt_archivesInfo_avianCorral, "禽畜栏_content");

		addTextView(R.id.txt_guominshi_qita, "其他药物过敏史");
		addTextView(R.id.txt_jibingshi_qita, "其他疾病史");
		addTextView(R.id.txt_jiazubing_qita_fuqin, "其他家族病史_父亲");
		addTextView(R.id.txt_jiazubing_qita_muqin, "其他家族病史_母亲");
		addTextView(R.id.txt_jiazubing_qita_xiongdi, "其他家族病史_兄弟姐妹");
		addTextView(R.id.txt_jiazubing_qita_zinv, "其他家族病史_子女");
		addTextView(R.id.txt_canji_qita, "其他残疾");

		operation = (TextView) findViewById(R.id.txt_archivesInfo_operation);
		trauma = (TextView) findViewById(R.id.txt_archivesInfo_trauma);
		transfusionBlood = (TextView) findViewById(R.id.txt_archivesInfo_transfusionBlood);

		setButtonListener(this, R.id.btn_back);

		Properties prop = new Properties();
		try {
			File f = new File(Environment.getExternalStorageDirectory()
					.getPath() + "/health/" + fileName + ".txt");
			if (f.exists()) {

				controlManager.setPropertiesPath(f.getAbsolutePath());
				controlManager.toControl();

				prop.load(new FileReader(f.getAbsolutePath()));
				operation.setText(getPropertiesValueByNames(prop, "手术名称1",
						"手术名称2"));
				trauma.setText(getPropertiesValueByNames(prop, "外伤名称1", "外伤名称2"));
				transfusionBlood.setText(getPropertiesValueByNames(prop,
						"输血原因1", "输血原因2"));
			} else {
				this.toastInformation(R.string.record_deleted);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @param prop
	 * @param strings
	 * @return
	 */
	public String getPropertiesValueByNames(Properties prop, String... strings) {
		StringBuffer buffer = new StringBuffer();
		try {
			for (String name : strings) {
				if ("手术名称1".equals(name)
						&& Util.allIsNotEmpty(prop.getProperty("手术名称1"))) {
					buffer.append(prop.getProperty("手术名称1"));
					if (Util.allIsNotEmpty(prop.getProperty("手术日期1"))) {
						buffer.append("-");
						buffer.append(prop.getProperty("手术日期1"));
					}
					buffer.append("  ");
				} else if ("手术名称2".equals(name)
						&& Util.allIsNotEmpty(prop.getProperty("手术名称2"))) {
					buffer.append(prop.getProperty("手术名称2"));
					if (Util.allIsNotEmpty(prop.getProperty("手术日期2"))) {
						buffer.append("-");
						buffer.append(prop.getProperty("手术日期2"));
					}
					buffer.append("  ");
				} else if ("外伤名称1".equals(name)
						&& Util.allIsNotEmpty(prop.getProperty("外伤名称1"))) {
					buffer.append(prop.getProperty("外伤名称1"));
					if (Util.allIsNotEmpty(prop.getProperty("外伤日期1"))) {
						buffer.append("-");
						buffer.append(prop.getProperty("外伤日期1"));
					}
					buffer.append("  ");
				} else if ("外伤名称2".equals(name)
						&& Util.allIsNotEmpty(prop.getProperty("外伤名称2"))) {
					buffer.append(prop.getProperty("外伤名称2"));
					if (Util.allIsNotEmpty(prop.getProperty("外伤日期2"))) {
						buffer.append("-");
						buffer.append(prop.getProperty("外伤日期2"));
					}
					buffer.append("  ");
				} else if ("输血原因1".equals(name)
						&& Util.allIsNotEmpty(prop.getProperty("输血原因1"))) {
					buffer.append(prop.getProperty("输血原因1"));
					if (Util.allIsNotEmpty(prop.getProperty("输血日期1"))) {
						buffer.append("-");
						buffer.append(prop.getProperty("输血日期1"));
					}
					buffer.append("  ");
				} else if ("输血原因2".equals(name)
						&& Util.allIsNotEmpty(prop.getProperty("输血原因2"))) {
					buffer.append(prop.getProperty("输血原因2"));
					if (Util.allIsNotEmpty(prop.getProperty("输血日期2"))) {
						buffer.append("-");
						buffer.append(prop.getProperty("输血日期2"));
					}
					buffer.append("  ");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return buffer.toString();
	}

	@Override
	public void onClick(View v) {
		int clickId = (Integer) v.getId();
		switch (clickId) {
		case R.id.btn_back:
			finish();
			break;
		default:
			break;
		}
	}
}
